package cn.itcast.service.impl;

import cn.itcast.domain.User;
import cn.itcast.mapper.UserMapper;
import cn.itcast.service.UserService;
import cn.itcast.utils.MapperFactory;
import cn.itcast.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class UserServiceImpl implements UserService {
   // private UserMapper mapper = (UserMapper) MapperUtils.getMapper(UserMapper.class);

    public List<User> findAll() {
        SqlSession sqlSession = null;
        UserMapper mapper = null;
        List<User> userList = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
             userList = mapper.findAll();
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            //事务回滚
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }

        return userList;

    }

    //查询所有
    public Integer add(String username, String email) {
        SqlSession sqlSession = null;
        UserMapper mapper = null;
        Integer i = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            i = mapper.add(username,email);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
            TransactionUtil.close(sqlSession);
        }

        return i;

    }

    //删除用户
    public Integer delete(String uid) {

        SqlSession sqlSession = null;
        UserMapper mapper = null;
        Integer i = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            i = mapper.delete(uid);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
            TransactionUtil.close(sqlSession);
        }

        return i;


    }

    @Override
    public User findById(String uid) {
        SqlSession sqlSession = null;
        UserMapper mapper = null;
        User user = null;
        try{
            sqlSession = MapperFactory.getSqlSession();
            mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            user =  mapper.findById(uid);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
            TransactionUtil.close(sqlSession);
        }

        return user;
    }

    @Override
    public Integer update(int id, String username, String email) {
        SqlSession sqlSession = null;
        UserMapper mapper = null;
        Integer i = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            i = mapper.update(id, username, email);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
            TransactionUtil.close(sqlSession);
        }

        return i;

    }

    //批量删除
    public Integer deleteByIds(String ids) {
        SqlSession sqlSession = null;
        UserMapper mapper = null;
        Integer i = null;
        if (ids != null && ids.length()>0){
            try{
                String[] arr = ids.split(",");
                sqlSession = MapperFactory.getSqlSession();
                mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
                 i = mapper.deleteByIds(arr);
                TransactionUtil.commit(sqlSession);
            }catch (Exception e){
                TransactionUtil.rollback(sqlSession);
                e.printStackTrace();
            }finally {
                TransactionUtil.close(sqlSession);
            }

        }
        return i;

    }
}
